#include "myTwoOptNext.h"
#include "mySolution.h"
#include "myTwoOpt.h"

myTwoOptNext::myTwoOptNext()
  : graph(NULL)
{
}

myTwoOptNext::myTwoOptNext(const Graph &g)
  : graph(new Graph(g))
{
}

myTwoOptNext::myTwoOptNext(const myTwoOptNext &m)
  : graph(new Graph(*(m.graph)))
{
}

myTwoOptNext::~myTwoOptNext()
{
  if (graph != NULL)
  {
    delete graph;
  }
}

edaMoveGen* myTwoOptNext::clone() const
{
  return new myTwoOptNext(*this);
}

bool myTwoOptNext::generate(edaMove *move, const edaSolution &sol)
{
  myTwoOpt *mymove = (myTwoOpt *)move;

  if ((mymove->first == graph->size() - 4) && 
      (mymove->second == mymove->first + 2))
  {
    return false;
  }
  else
  {
    mymove->second++;
    if (mymove->second == (graph->size() - 1))
    {
      mymove->first++;
      mymove->second = mymove->first + 2;
    }
  }
  return true;
}

void myTwoOptNext::Serialize(edaBuffer &buf, bool pack)
{
  edaMoveGen::Serialize(buf, pack);

  if (pack)
  {
    graph->doSerialize(buf, pack);
  }
  else
  {
    if (graph != NULL)
    {
      delete graph;
    }
    graph = (Graph *)classGenerateFromBuffer(buf);
  }
}
